package com.sgb.kjwl.viewmodel.manage;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.service.WebsiteInfoAPI;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.view.ui.fragment.manage.OperatingResultsForWebsite;
import com.sgb.member.entity.OperatingResultsEntity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class OperatingResultsForWebsiteModel extends BaseViewModel {

    private OperatingResultsForWebsite fragment;

    private int page = 1;
    private int limit = 10;

    public OperatingResultsForWebsiteModel(OperatingResultsForWebsite fragment) {
        this.fragment = fragment;
    }

    public void getOperatingResultsList(String siteNo, int siteType, boolean isLoadMore) {
        Map<String, String> map = new HashMap();
        page = isLoadMore ? page + 1 : 1;
        map.put("page", page + "");
        map.put("limit", limit + "");
        map.put("siteNo", siteNo);
        map.put("siteType", siteType + "");
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getWebsiteInfoAPI().performanceCertificateAdminList(WebsiteInfoAPI.WebsiteUrl.URL_SITE_PERFORMANCE_CERTIFICATE, map).enqueue(new ICallBack<BaseEntity<ListResponse<List<OperatingResultsEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<OperatingResultsEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    if (data.getData().isLastPage()) {
                        fragment.showList(data.getData().getList(), isLoadMore, true);
                    } else {
                        fragment.showList(data.getData().getList(), isLoadMore, false);
                    }
                }
            }
        });
    }

    // -------------------------------- 工人详情H5链接 --------------------------------

    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("usermanage").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id;
                    MLog.d("api", "H5跳转链接  " + url);
                    fragment.getUrl(url);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
